﻿using AutoMapper;
using DFGK.IRepository;
using DFGK.IService;
using DFGK_WMS.Model.Dto;
using DFGK_WMS.Model.Models;
using NPOI.SS.Formula.Functions;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DFGK.Service
{
    public class EquipmentBasicService : BaseService, IEquipmentBasicService
    {
        private readonly IEquipmentBasicRepository _equipmentBasicRepository;
        private readonly IUnitOfWork _unitOfWork;
        public EquipmentBasicService(IMapper mapper, IEquipmentBasicRepository _equipmentBasicRepository, IUnitOfWork _unitOfWork) : base(mapper)
        {
            this._equipmentBasicRepository = _equipmentBasicRepository;
            this._unitOfWork = _unitOfWork;
        }

        public bool AddEquipmentBasic(EquipmentBasic equipmentBasic)
        {
            _equipmentBasicRepository.AddEntity(equipmentBasic);
            return _unitOfWork.SaveChangesEntity();
        }

        public bool DelEquipmentBasic(long id)
        {
            var cust = _equipmentBasicRepository.QueryEntityById(id);
            if (cust !=null)
            {
                _equipmentBasicRepository.DelEntity(cust);
                return _unitOfWork.SaveChangesEntity();
            }
            return false;
        }

        public bool AddEquipment(List<EquipmentBasic> list)
        {
            int count = 0;
            foreach (var item in list)
            {
                _equipmentBasicRepository.AddEntity(item);
                _unitOfWork.SaveChangesEntity();
                count++;
            }
            if(count <= 0)
            {
                return false;
            }
            return true;
        }

        public List<EquipmentBasicDown> GetEquipmentBasic()
        {
            var allMsg = _equipmentBasicRepository.GetAllEntityList();
            List<EquipmentBasicDown> list = new List<EquipmentBasicDown>();
            foreach (var item in allMsg)
            {
                EquipmentBasicDown equipmentBasicDown = new EquipmentBasicDown();
                equipmentBasicDown.Name = item.Name;
                equipmentBasicDown.Code = item.Code;
                list.Add(equipmentBasicDown);
            }
            return list;
        }

        public List<EquipmentBasic> GetPurchaseOrderDown()
        {
            var list = _equipmentBasicRepository.GetAllEntityList();
            return list;
        }

        public List<EquipmentBasicMsgDown> GetEquipmentBasicMsgDowns()
        {
            var allMsg = _equipmentBasicRepository.GetAllEntityList();
            List<EquipmentBasicMsgDown> list = new List<EquipmentBasicMsgDown>();
            foreach (var item in allMsg)
            {
                EquipmentBasicMsgDown equipmentBasicDown = new EquipmentBasicMsgDown();
                equipmentBasicDown.Name = item.Name;
                equipmentBasicDown.Code = item.Code;
                equipmentBasicDown.ModelName = item.ModelName;
                equipmentBasicDown.SpecificationModel = item.SpecificationModel;
                list.Add(equipmentBasicDown);
            }
            return list;
        }

        public List<EquipmentBasic> GetEquipmentBasic(string? name, int pageIndex, int pageSize, out int totalCount)
        {
            var custList = _equipmentBasicRepository.GetEntityAlls();
            if (!string.IsNullOrEmpty(name))
            {
                custList = custList.Where(c => c.Name.Contains(name));
            }
            var list = _equipmentBasicRepository.GetEntityAlls(custList, pageIndex, pageSize, out totalCount, x => x.CreateDate, false);
            return list.ToList();
        }

        public EquipmentBasic? GetEquipmentBasicById(long id)
        {
            return _equipmentBasicRepository.QueryEntityById(id);
        }

        public bool UpdEquipmentBasic(EquipmentBasic EquipmentBasic)
        {
            var aobj = _equipmentBasicRepository.QueryIsEntityWhere(x => x.Id == EquipmentBasic.Id);
            if (aobj == null)
            {
                return false;
            }
            _equipmentBasicRepository.UpdEntity(EquipmentBasic);
            return _unitOfWork.SaveChangesEntity();
        }
    }
}
